﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using GameOn.Web.Entities;
using GameOn.Web.Services;

namespace GameOn.Web.Controllers
{
    /// <summary>
    /// A controller for Player related actions
    /// </summary>
    public class PlayersController : Controller
    {
        private GameOnEntities Entities { get; set; }
        private IPlayerService PlayerService { get; set; }

        public PlayersController()
        {
            Entities = new GameOnEntities();
            PlayerService = new PlayerService(Entities);
        }

        /// <summary>
        /// Returns a list of Players in order of their place on the ladder
        /// </summary>
        [HttpGet]
        public ActionResult Ladder()
        {
            ViewData.Model = PlayerService.GetListOfPlayersWithMatchSummaries();
            return View("Ladder_m");
        }

        /// <summary>
        /// Gets Player's details and match summaries as CSV
        /// </summary>
        [HttpGet]
        public ActionResult GetPlayersCsv()
        {
            ViewData.Model = PlayerService.GetListOfPlayersWithMatchSummaries();
            return View(new PlayerCsvView("players.csv"));
        }

        /// <summary>
        /// Gets Sparklines data for optionally one or all players
        /// </summary>
        /// <returns>Sparklines data as JSON</returns>
        [HttpGet]
        public JsonResult GetSparklinesData(int? playerId)
        {
            List<Tuple<int, string>> data;
            if (playerId.HasValue)
            {
                data = new List<Tuple<int, string>> {PlayerService.GetRankHistoryDataForSparkLine(playerId.Value)};
            }
            else
            {
                data = PlayerService.GetRankHistoryDataForSparkLine();
            }

            return new JsonResult {Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
        }
    }
}
